<template>
  <div class="count-box">
    <button @click="handleAdd" class="add">+</button>
    <input class="inp" type="text" :value="value" @change="handleChange">
    <button @click="handleSub" class="minus">-</button>
  </div>
</template>

<script>
export default {
  // 接收父传子数据
  props: {
    value: {
      type: Number,
      default: 1
    }
  },
  // 自定义方法
  methods: {
    handleAdd () {
      this.$emit('input', this.value + 1)
    },
    handleSub () {
      if (this.value <= 1) {
        return
      }
      this.$emit('input', this.value - 1)
    },
    handleChange (e) {
      const num = e.target.value
      if (isNaN(num) || num < 1) {
        e.target.value = this.value
        return
      }
      this.$emit('input', num)
    }
  }

}
</script>

<style lang="less" scoped>
    .count-box{
        width: 110px;
        display: flex;
        .add,.minus{
            width: 40px;
            height: 30px;
            outline: none;
            border: none;
            background-color: #efefef;
        }
        .inp{
            width: 40px;
            height: 30px;
            outline: none;
            border: none;
            margin: 0 5px;
            background-color: #efefef;
            text-align: center;
        }
    }

</style>
